/**
 * @param {number[][]} triangle
 * @return {number}
 * @description 第二次做
 */

 var minimumTotal = function (triangle) {
    if (!triangle || !triangle.length || !triangle[0].length) {
        return 0
    }
    let row = triangle.length
    let res = new Array(row)
    for (let i = 0; i < row; i++) {
        res[i] = new Array(triangle[i].length)
    }
    res[0][0] = triangle[0][0]
    for (let i = 1; i < row; i++) {
        for (let j = 0; j < triangle[i].length; j++) {
            // 如果为第一列
            if (j === 0) {
                res[i][j] = res[i - 1][j] + triangle[i][j]
            }
            // 如果为一行最后一个元素
            else if (j === triangle[i].length - 1) {
                res[i][j] = triangle[i][j] + res[i - 1][j - 1]
            }
            // 如果是中间元素
            else {
                res[i][j] = triangle[i][j] + Math.min(res[i - 1][j - 1], res[i - 1][j])
            }
        }
    }
    return Math.min(...res[row - 1])
};